﻿using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class HomeController : Base
    {
        private readonly ArticleService _articleService;
        private readonly LogService _logService;
        private readonly LoginUtil _LoginUtil;
        private readonly NoticeKeyService _noticeKey;
        private readonly ArticleExamineService _articleExamineService;

        public HomeController(
            ArticleService articleService,
            LoginUtil LoginUtil,
            LogService logService,
            NoticeKeyService noticeKey,
            ArticleExamineService articleExamineService
            )
        {
            _articleService = articleService;
            _LoginUtil = LoginUtil;
            _logService = logService;
            _noticeKey = noticeKey;
            _articleExamineService = articleExamineService;
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Menu_Index)]//这里为了配合菜单显示，用Menu_Index 控制权限  下同
        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Menu_Index)]
        public async Task<IActionResult> HomeAsync(int view = 0)
        {
            int userId = await _LoginUtil.LoginUserId();
            if (view == 1)
            {
                List<ArticleCount> forUserMonth = await _articleService.CountArticleForUserAsync(true);
                NumCount countAll = await _articleService.CountAllAsync();
                //统计当前用户等审核文件数
                long countExamine = await _articleExamineService.CountExamineAsync(userId);
                return Json(new { status = 1, forUserMonth,  countAll,  countExamine });
            }

            List<ViewLogSql> logList = await _logService.GetListAsync(userId);//显示3条
            ViewBag.logList = logList;
            return View();
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Menu_Index)]
        public async Task<IActionResult> CountAsync()
        {
            int userId = await _LoginUtil.LoginUserId();
            long notice = await _noticeKey.CountAsync(userId);//统计消息未读
            long examine = await _articleExamineService.CountExamineAsync(userId);//统计待审核

            return Json(new { status = 1, notice, examine });

        }

    }
}
